package com.kitty.game.player.serializer;

import com.kitty.game.friend.model.FriendBox;
import com.kitty.game.guard.model.GuardBox;
import com.kitty.game.role.model.Role;
import com.kitty.game.utils.JsonUtils;
import org.apache.commons.lang3.StringUtils;

public class CommonBoxSerializer implements IPlayerPropSerializer {

    @Override
    public void serialize(Role player) {
        if (player.getFriendBox() != null) {
            player.setFriendBoxJson(JsonUtils.object2String(player.getFriendBox()));
        }

        if (player.getGuardBox() != null) {
            player.setGuardBoxJson(JsonUtils.object2String(player.getGuardBox()));
        }

    }

    @Override
    public void deserialize(Role player) {
        String friendJson = player.getFriendBoxJson();
        if (StringUtils.isNotEmpty(friendJson)) {
            player.setFriendBox(JsonUtils.string2Object(friendJson, FriendBox.class));
        }

        String guardsJson = player.getGuardBoxJson();
        if (StringUtils.isNotEmpty(guardsJson)) {
            player.setGuardBox(JsonUtils.string2Object(guardsJson, GuardBox.class));
        }
    }
}
